package com.fanshuai.spring.boot.service;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {
    @Resource
    private RedisTemplate<String, String> redisTemplate;

    public boolean expire(String key, long time) {
        if (time > 0) {
            redisTemplate.expire(key, time, TimeUnit.SECONDS);
        }
        return true;
    }

    public Long getExpire(String key) {
        return redisTemplate.getExpire(key, TimeUnit.SECONDS);
    }

    public boolean exists(String key) {
        Boolean b = redisTemplate.hasKey(key);
        return null != b && b;
    }

    public void del(String ...key) {
        if (key.length == 1) {
            redisTemplate.delete(key[0]);
        } else {
            redisTemplate.delete(Arrays.asList(key));
        }
    }

    public String get(String key) {
        if (StringUtils.isBlank(key)) {
            return null;
        }
        return redisTemplate.opsForValue().get(key);
    }

    public boolean set(String key, String value) {
        if (StringUtils.isNotBlank(key)) {
            redisTemplate.opsForValue().set(key, value);
        }
        return true;
    }

    public boolean set(String key, String value, long expire) {
        if (StringUtils.isNotBlank(key)) {
            redisTemplate.opsForValue().set(key, value, expire, TimeUnit.SECONDS);
        }
        return true;
    }

    public boolean setNxEx(String key, String value, long expire) {
        if (StringUtils.isNotBlank(key)) {
            redisTemplate.opsForValue().setIfAbsent(key, value, Duration.ofSeconds(expire));
        }
        return true;
    }

    public Long incr(String key, long delta) {
        if (delta < 0) {
            throw new RuntimeException("递增因子必须大于0");
        }
        return redisTemplate.opsForValue().increment(key, delta);
    }

    public Long decr(String key, long delta) {
        return incr(key, -delta);
    }

    public String hget(String key, String hashKey) {
        return (String) redisTemplate.opsForHash().get(key, hashKey);
    }

    public Map hmget(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    public boolean hmset(String key, Map<String, Object> map) {
        redisTemplate.opsForHash().putAll(key, map);
        return true;
    }

    public boolean hmset(String key, Map<String, Object> map, long expire) {
        redisTemplate.opsForHash().putAll(key, map);
        if (expire > 0) {
            exists(key);
        }
        return true;
    }

    public boolean hset(String key, String hashKey, String value) {
        redisTemplate.opsForHash().put(key, hashKey, value);
        return true;
    }

    public boolean hset(String key, String hashKey, String value, long expire) {
        redisTemplate.opsForHash().put(key, hashKey, value);
        if (expire > 0) {
            exists(key);
        }
        return true;
    }

    public void hdel(String key, String... hashKeys) {
        redisTemplate.opsForHash().delete(key, hashKeys);
    }

    public boolean hHasKey(String key, String hashKey) {
        return redisTemplate.opsForHash().hasKey(key, hashKey);
    }

    public double hincr(String key, String hashKey, double delta) {
        return redisTemplate.opsForHash().increment(key, hashKey, delta);
    }

    public double hdecr(String key, String hashKey, double delta) {
        return redisTemplate.opsForHash().increment(key, hashKey, -delta);
    }

    public Set<String> sMembers(String key) {
        return redisTemplate.opsForSet().members(key);
    }

    public boolean sHasKey(String key, String value) {
        Boolean b = redisTemplate.opsForSet().isMember(key, value);
        return null != b && b;
    }





}
